package com.huangguolin.cloud.role.control;

import com.huangguolin.cloud.mybatis.entity.PtRoleEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.role.entity.SearchRoleEntity;
import com.huangguolin.cloud.role.service.RoleService;
import com.huangguolin.cloud.spring.control.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private RoleService roleService;
    @Secured("ROLE_cldk_data_role_query")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/queryRoleListByPage")
    @ResponseBody
    public PadingRstEntity<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingEntity paging){
        logger.info(entity);
        logger.info(paging);

        PadingRstEntity<PtRoleEntity> ptRoleEntityPadingRstEntity = roleService.queryRoleListByPage(entity,paging);
        return ptRoleEntityPadingRstEntity;
    }
    @RequestMapping("/queryMenuZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryMenuZtreeAll(){
        List<ZtreeEntity> list = roleService.queryMenuZtreeAll();
        return list;
    }

    @RequestMapping("/saveMenuRefRole")
    @ResponseBody
    public String saveMenuRefRole(@RequestParam("menuIds") String menuIds, @RequestParam("roleIds")String roleIds){

        logger.info("menuIds:" + menuIds + "roleIds" + roleIds);
        roleService.saveMenuRefRole(menuIds,roleIds);
        return successJSON("role.allow.menu.success");
    }

    @RequestMapping("/queryMenuZtreeByRole")
    @ResponseBody
    public List<ZtreeEntity> queryMenuZtreeByRole(@RequestParam("roleId")String roleId){
        logger.info("roleId" + roleId);
        List<ZtreeEntity> list = roleService.queryMenuZtreeByRole(roleId);
        return list;
    }

    //角色增加控制层
    @RequestMapping("/addRole")
    @ResponseBody
    public String addRole(@RequestParam("roleName")String roleName,@RequestParam("remarks")String remarks) {

        roleService.addRole(roleName,remarks);
        return successJSON("add.role.success");
    }

    //角色删除控制层
    @RequestMapping("/deleteRole")
    @ResponseBody
    public String deleteRole(@RequestParam("roleUuid")String roleUuid) {
        roleService.deleteRole(roleUuid);
        return successJSON("delete.role.success");
    }

    //角色修改控制层
    @RequestMapping("/modifyRole")
    @ResponseBody
    public String modifyRole(@RequestParam("roleUuid")String roleUuid,@RequestParam("roleName")String roleName,@RequestParam("remarks")String remarks) {
        roleService.modifyRole(roleUuid,roleName,remarks);
        return successJSON("modify.role.success");
    }


    @RequestMapping("/getRoleAuthority")
    @ResponseBody
    public List<ZtreeEntity> getRoleAuthority(@RequestParam("roleUuid")String roleUuid){
        List<ZtreeEntity> list = roleService.getRoleAuthority(roleUuid);
        return list;
    }


}
